﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Brjnk.GpxExplorer.UI.Controls
{
    /// <summary>
    /// Interaction logic for ColorDialog.xaml
    /// </summary>
    public partial class ColorDialog : Window, INotifyPropertyChanged
    {
        public ColorDialog(Color color)
        {
            InitializeComponent();

            txbR.Text = color.R.ToString();
            txbG.Text = color.G.ToString();
            txbB.Text = color.B.ToString();

            GenerateTable();
        }

        private void GenerateTable()
        {
            GenerateRow(1, 1, 1, 8, true);
            GenerateRow(0, 0, 1, 8, false);
            GenerateRow(0, 1, 0, 8, false);
            GenerateRow(1, 0, 0, 8, false);
            GenerateRow(1, 1, 0, 8, false);
            GenerateRow(0, 1, 1, 8, false);
            GenerateRow(1, 0, 1, 8, false);
        }

        private void GenerateRow(double r, double g, double b, int count, bool includeZero)
        {
            ;
            int startIndex = includeZero ? 0 : 1;

            double rCoefficient = r / ((double)count + startIndex);
            double gCoefficient = g / ((double)count + startIndex);
            double bCoefficient = b / ((double)count + startIndex);

            for (int i = startIndex; i < startIndex + count; i++)
            {
                byte red = (byte)((i * rCoefficient) * 255);
                byte green = (byte)((i * gCoefficient) * 255);
                byte blue = (byte)((i * bCoefficient) * 255);
                table.Children.Add(CreateButton(Color.FromArgb(255, red, green, blue)));
            }
        }

        private Button CreateButton(Color color)
        {
            var btn = new Button();
            btn.Background = new SolidColorBrush(color);
            btn.Width = 30;
            btn.Height = 30;
            btn.MinWidth = 30;
            btn.CommandParameter = color;
            btn.Click += new RoutedEventHandler(btn_Click);
            return btn;
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            var color = (Color)((Button)sender).CommandParameter;
            this.Color = color;
            this.DialogResult = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            byte r = 0; byte g = 0; byte b = 0;
            if (!byte.TryParse(txbR.Text, out r)) { txbR.SelectAll(); return; }
            if (!byte.TryParse(txbG.Text, out g)) { txbG.SelectAll(); return; }
            if (!byte.TryParse(txbB.Text, out b)) { txbB.SelectAll(); return; }
            this.Color = Color.FromArgb(255, r, g, b);
            this.DialogResult = true;
        }

        private Color color;

        public Color Color
        {
            get { return color; }
            set { color = value; OnPropertyChanged("Color"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}